home *** CD-ROM | disk | FTP | other *** search
- Newsgroups: comp.lang.c
- Path: link-1.ts.bcc.ac.uk!reavpar
- From: reavpar@ucl.ac.uk (Mr Philip Arthur Riebold)
- Subject: Re: How to get a random string each time??
- Message-ID: <1996Feb22.123954.80675@ucl.ac.uk>
- Date: Thu, 22 Feb 1996 12:39:54 GMT
- References: <4fh5od$qq0@news.nevada.edu> <4g4c5m$tlb@news.nevada.edu> <1996Feb21.150754.81848@ucl.ac.uk>
- Organization: University College London
-
- reavpar@ucl.ac.uk (Mr Philip Arthur Riebold) writes:
-
- >int main(void)
- >{
- > char line[BUFSIZ], it[BUFSIZ];
- > int n;
- > srand48(time(NULL)); /* or seed the generator in some other way */
- > for(n = 1; gets(line); n++) /*probably better to use fgets()*/
- > if((n * drand48()) < 1.0) /*drand returns a double in [0.0, 1.0) */
-
- Oops !, sorry I made a typo, the above line should (of course !) be
- if(drand48() < (1.0/n))
-
- > strcpy(it, line);
- > puts(it);
- >}
-
- BTW thinking about it the code really _should_ have used fgets()
- --
- Philip Riebold,
-
-